package com.jieao.template.mapper;

import com.jieao.template.entity.roleMgr.RoleDO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * 角色表(Role)表数据库访问层
 *
 * @author jzw
 * @since 2020-04-30 09:31:15
 */
public interface RoleMapper
{
    /**
     * 获取当前登录用户的角色
     *
     * @param userId
     * @return java.util.List<java.lang.String>
     * @author Jzw
     * @date 2019/12/27 15:46
     */
    @Select("SELECT " +
            " r.name  " +
            "FROM " +
            " account_role ar " +
            " LEFT JOIN role r ON r.id = ar.role_id  " +
            "WHERE " +
            " ar.account_id = #{_parameter} " +
            " and ar.deleted=false " +
            " and r.deleted=false" +
            " and r.enable=true"
    )
    List<String> getAllRolesByUserId(String userId);

    /**
     * 移除当前角色所有和权限的关系
     *
     * @param id
     * @return void
     * @author 姜兆伟
     * @date 2019/8/30 15:45
     */
    @Update("update role_permission set deleted=true where role_id=#{id}")
    void removeRolePermissionForRole(String id);

    /**
     * 给角色分配权限
     *
     * @param dataList
     * @return void
     * @author Jzw
     * @since 2020/5/25 20:06
     */
    @Insert(" <script>replace INTO role_permission( role_id, permission_id,checked,id)" +
            "VALUES" +
            "<foreach collection=\"list\" item=\"item\" separator=\",\">" +
            "(" +
            "#{item.roleId},#{item.permissionId},#{item.checked},#{item.id}" +
            ")" +
            "</foreach></script> ")
    void assignmentPermissionForRole(List<Map<String, Object>> dataList);

    /**
     * 启禁用角色
     *
     * @param roleId 角色id
     * @return java.lang.Object
     * @author Jzw
     * @date 2019/12/13 13:50
     */
    @Update("update role set enable = not enable where id=#{_parameter}")
    int disOrEnaAccount(String roleId);

    /**
     * 获取用户关联的角色
     *
     * @param id 用户id
     * @return java.lang.Object
     * @author Jzw
     * @date 2019/12/30 11:09
     */
    @Select("select role_id from account_role where account_id=#{_parameter} and deleted=false")
    String getRoleByUserId(String id);

    /**
     * 获取当前角色已选的权限
     *
     * @param id 角色id
     * @return java.util.List<java.lang.String>
     * @author Jzw
     * @date 2019/12/17 16:21
     */
    @Select("SELECT " +
            "permission_id " +
            "FROM " +
            "role_permission " +
            "WHERE " +
            "role_id = #{_parameter} " +
            "AND checked = TRUE " +
            "AND deleted = FALSE")
    List<String> listCheckedPermissionForRole(String id);

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     * @author jzw
     * @since 2020-04-30 09:31:15
     */
    @Select("select " +
            "id," +
            "name," +
            "description," +
            "deleted," +
            "enable," +
            "create_time," +
            "create_id," +
            "update_time," +
            "update_id " +
            "from role where deleted =false and enable =true  and id = #{_parameter}"
    )
    RoleDO queryById(String id);

    /**
     * 查询分页数据
     *
     * @param params 查询筛选条件
     * @return 对象列表
     * @author jzw
     * @since 2020-04-30 09:31:15
     */
    @Select("<script>select " +
            "id," +
            "name," +
            "description," +
            "deleted," +
            "enable," +
            "create_time," +
            "create_id," +
            "update_time," +
            "update_id " +
            "from role " +
            "where deleted =false " +
            "<if test=\"name != null and name != ''\">" +
            "and name = #{name} " +
            "</if>" +
            "<if test=\"description != null and description != ''\">" +
            "and description = #{description} " +
            "</if>" +
            "<if test=\"enable != null\">" +
            "and enable = #{enable} " +
            "</if>" +
            "<if test=\"createTime != null\">" +
            "and create_time = #{createTime} " +
            "</if>" +
            "<if test=\"createId != null and createId != ''\">" +
            "and create_id = #{createId} " +
            "</if>" +
            "<if test=\"updateTime != null\">" +
            "and update_time = #{updateTime} " +
            "</if>" +
            "<if test=\"updateId != null and updateId != ''\">" +
            "and update_id = #{updateId} " +
            "</if>" +
            "</script>"
    )
    List<RoleDO> queryAllByLimit(RoleDO params);


    /**
     * 通过实体作为筛选条件查询
     *
     * @param role 实例对象
     * @return 对象列表
     * @author jzw
     * @since 2020-04-30 09:31:15
     */
    @Select("<script>select " +
            "id," +
            "name," +
            "description," +
            "deleted," +
            "enable," +
            "create_time," +
            "create_id," +
            "update_time," +
            "update_id " +
            "from role " +
            "where deleted =false " +
            "<if test=\"name != null and name != ''\">" +
            "and name = #{name} " +
            "</if>" +
            "<if test=\"description != null and description != ''\">" +
            "and description = #{description} " +
            "</if>" +
            "<if test=\"enable != null\">" +
            "and enable = #{enable} " +
            "</if>" +
            "<if test=\"createTime != null\">" +
            "and create_time = #{createTime} " +
            "</if>" +
            "<if test=\"createId != null and createId != ''\">" +
            "and create_id = #{createId} " +
            "</if>" +
            "<if test=\"updateTime != null\">" +
            "and update_time = #{updateTime} " +
            "</if>" +
            "<if test=\"updateId != null and updateId != ''\">" +
            "and update_id = #{updateId} " +
            "</if>" +
            "</script>"
    )
    List<RoleDO> queryAll(RoleDO role);

    /**
     * 新增数据
     *
     * @param role 实例对象
     * @return 影响行数
     * @author jzw
     * @since 2020-04-30 09:31:15
     */
    @Insert("insert into role(id, name, description, enable, create_time, create_id, update_time, update_id)" +
            " values (#{id}, #{name}, #{description}, #{enable}, now(), #{createId}, #{updateTime}, #{updateId})")
    void insert(RoleDO role);

    /**
     * 修改数据
     *
     * @param role 实例对象
     * @return 影响行数
     * @author jzw
     * @since 2020-04-30 09:31:15
     */
    @Update("<script>update role " +
            "<set>" +
            " <if test=\"name != null and name != ''\">" +
            " name = #{name} , " +
            " </if>" +
            " <if test=\"description != null and description != ''\">" +
            " description = #{description} , " +
            " </if>" +
            " <if test=\"enable != null\">" +
            " enable = #{enable} , " +
            " </if>" +
            " <if test=\"createId != null and createId != ''\">" +
            " create_id = #{createId} , " +
            " </if>" +
            " <if test=\"updateTime != null\">" +
            " update_time = now(), " +
            " </if>" +
            " <if test=\"updateId != null and updateId != ''\">" +
            " update_id = #{updateId}  " +
            " </if>" +
            "</set>" +
            "where deleted = false and id = #{id}</script>")
    int update(RoleDO role);

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 影响行数
     * @author jzw
     * @since 2020-04-30 09:31:15
     */
    @Delete("update role set deleted = true where id = #{_parameter}")
    int deleteById(String id);

    /**
     * 通过主键集合批量删除数据
     *
     * @param idList 主键集合
     * @author jzw
     * @since 2020-04-30 09:31:15
     */
    @Delete("<script>" +
            "<foreach separator=\";\" collection=\"list\" item=\"item\" >" +
            "update role set deleted =true where id =#{item}" +
            "</foreach></script>")
    void batchDeletionByIdList(List<String> idList);

}